package com.pmcc.cj.task;
import com.pmcc.cj.entity.CjOrder;
import com.pmcc.cj.entity.CjPosition;
import com.pmcc.cj.entity.CjTask;
import com.pmcc.cj.repository.CjOrderRepository;
import com.pmcc.cj.repository.CjPositionRepository;
import com.pmcc.cj.repository.CjTaskRepository;
import com.pmcc.cj.service.CjTaskService;
import com.pmcc.core.repository.DictRepository;
import com.pmcc.core.service.EmployeeService;
import com.pmcc.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author 姚楠
 * @Title: 定时任务
 * @Description: TODO
 * @date 2017/12/26 17:56
 */
@Component
public class datScheduled {

    private static final Logger logger = LoggerFactory.getLogger(datScheduled.class);

    @Autowired
    CjPositionRepository cjPositionRepository;
    @Autowired
    DictRepository dictRepository;
    @Autowired
    CjTaskService cjTaskService;
    @Autowired
    CjTaskRepository cjTaskRepository;
    @Autowired
    CjOrderRepository cjOrderRepository;

    /**
     * 凌晨一点执行，删除超过20条的定位记录
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void updatePosition(){
        List list = cjPositionRepository.findByCustomerGroup();
        if(list != null && list.size()>0){
            for(int j=0;j<list.size();j++){
                List<CjPosition> lt = new ArrayList<>();
                String str=list.get(j).toString();
                List<CjPosition> ls = cjPositionRepository.findByCustomerIdOrderByCreateTimeDesc(str);
                if(ls != null && ls.size()>20){
                    for(int i=20;i<ls.size();i++){
                        lt.add(ls.get(i));
                    }
                }
                if(lt != null && lt.size()>0){
                    for(int i=0;i<lt.size();i++){
                        cjPositionRepository.delete(lt.get(i));
                    }
                }
            }
        }
    }

    /**
     * 凌晨俩点执行，机手确认超出指定期限的自动分账
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void splitMoney(){
        int day = Integer.parseInt(dictRepository.findByCode("FZSJ").get(0).getName());
        List<CjOrder> list=cjOrderRepository.findByRobotAndCustomer();
        if(list != null && list.size()>0){
            for(CjOrder order: list){
                int num=DateUtil.betweenOfDay(order.getRobotConfirmTime(),new Date());
                if(num>day){
                    try {
                        order.setPayStatus(4);
                        order.setCustomerConfirm(1);
                        order.setCustomerConfirmTime(new Date());
                        order.setCustomerFlag(1);
                        cjOrderRepository.saveAndFlush(order);
                        CjTask task= cjTaskRepository.findByOrderId(order.getId());
                        cjTaskService.splitMoney(task);
                    }catch (Exception e){
                        System.out.println("分账失败！");
                    }
                }
            }
        }
    }

}
